﻿$Source = @"
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Reflection;

public class sRDI
{

    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    delegate IntPtr ReflectiveLoader();

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    delegate bool ExportedFunction(IntPtr userData, uint userLength);

    static uint Ror(uint val, int r_bits, int max_bits)
    {
        return (val >> r_bits) | (val << (max_bits - r_bits));
    }

    public static uint HashFunction(string name)
    {
        uint functionHash = 0;

        name += "\x00";

        foreach (char c in name)
        {
            functionHash = Ror(functionHash, 13, 32);
            functionHash += c;
        }

        return functionHash;
    }

    static unsafe bool Is64BitDLL(byte[] dllBytes)
    {
        bool is64Bit = false;

        GCHandle scHandle = GCHandle.Alloc(dllBytes, GCHandleType.Pinned);
        IntPtr scPointer = scHandle.AddrOfPinnedObject();

        Int32 headerOffset = Marshal.ReadInt32(scPointer, 60);
        UInt16 magic = (UInt16)Marshal.ReadInt16(scPointer, headerOffset + 4);

        if (magic == (UInt16)512 || magic == (UInt16)34404)
            is64Bit = true;

        scHandle.Free();

        return is64Bit;
    }

    public static byte[] ConvertToShellcode(byte[] dllBytes, uint functionHash, byte[] userData, uint flags)
    {
            //MARKER:S
            var rdiShellcode32 = new byte[] { 0x83,0xEC,0x6C,0x53,0x55,0x56,0x57,0xB9,0x4C,0x77,0x26,0x07,0xE8,0x6E,0x06,0x00,0x00,0x8B,0xF8,0xB9,0x49,0xF7,0x02,0x78,0x89,0x7C,0x24,0x28,0xE8,0x5E,0x06,0x00,0x00,0x8B,0xF0,0xB9,0x58,0xA4,0x53,0xE5,0x89,0x74,0x24,0x2C,0xE8,0x4E,0x06,0x00,0x00,0x8B,0xD8,0xB9,0x10,0xE1,0x8A,0xC3,0x89,0x5C,0x24,0x20,0xE8,0x3E,0x06,0x00,0x00,0xB9,0xAF,0xB1,0x5C,0x94,0x89,0x44,0x24,0x30,0xE8,0x30,0x06,0x00,0x00,0xB9,0x33,0x00,0x9E,0x95,0x89,0x44,0x24,0x34,0xE8,0x22,0x06,0x00,0x00,0xB9,0x44,0xF0,0x35,0xE0,0x8B,0xE8,0xE8,0x16,0x06,0x00,0x00,0x89,0x44,0x24,0x40,0x85,0xFF,0x0F,0x84,0x00,0x06,0x00,0x00,0x85,0xF6,0x0F,0x84,0xF8,0x05,0x00,0x00,0x85,0xDB,0x0F,0x84,0xF0,0x05,0x00,0x00,0x83,0x7C,0x24,0x30,0x00,0x0F,0x84,0xE5,0x05,0x00,0x00,0x83,0x7C,0x24,0x34,0x00,0x0F,0x84,0xDA,0x05,0x00,0x00,0x85,0xED,0x0F,0x84,0xD2,0x05,0x00,0x00,0x85,0xC0,0x0F,0x84,0xCA,0x05,0x00,0x00,0x8B,0x84,0x24,0x80,0x00,0x00,0x00,0x8B,0x70,0x3C,0x03,0xF0,0x81,0x3E,0x50,0x45,0x00,0x00,0x0F,0x85,0xB2,0x05,0x00,0x00,0xB8,0x4C,0x01,0x00,0x00,0x66,0x39,0x46,0x04,0x0F,0x85,0xA3,0x05,0x00,0x00,0xF6,0x46,0x38,0x01,0x0F,0x85,0x99,0x05,0x00,0x00,0x0F,0xB7,0x56,0x06,0x33,0xFF,0x0F,0xB7,0x46,0x14,0x85,0xD2,0x74,0x22,0x8D,0x4E,0x24,0x03,0xC8,0x83,0x79,0x04,0x00,0x8B,0x01,0x75,0x05,0x03,0x46,0x38,0xEB,0x03,0x03,0x41,0x04,0x3B,0xC7,0x0F,0x47,0xF8,0x83,0xC1,0x28,0x83,0xEA,0x01,0x75,0xE3,0x8D,0x44,0x24,0x58,0x50,0xFF,0xD5,0x8B,0x4C,0x24,0x5C,0x8D,0x51,0xFF,0x8D,0x69,0xFF,0xF7,0xD2,0x03,0x6E,0x50,0x8D,0x41,0xFF,0x03,0xC7,0x23,0xEA,0x23,0xC2,0x3B,0xE8,0x0F,0x85,0x42,0x05,0x00,0x00,0x6A,0x04,0xBF,0x00,0x30,0x00,0x00,0x57,0x55,0xFF,0x76,0x34,0xFF,0xD3,0x8B,0xD8,0x89,0x5C,0x24,0x24,0x85,0xDB,0x75,0x0F,0x6A,0x04,0x57,0x55,0x50,0xFF,0x54,0x24,0x30,0x8B,0xD8,0x89,0x44,0x24,0x24,0xF6,0x84,0x24,0x90,0x00,0x00,0x00,0x01,0x74,0x28,0x8B,0x94,0x24,0x80,0x00,0x00,0x00,0x8B,0x42,0x3C,0x89,0x43,0x3C,0x8B,0x4A,0x3C,0x3B,0x4E,0x54,0x73,0x31,0x8D,0x3C,0x0B,0x2B,0xD3,0x8A,0x04,0x3A,0x41,0x88,0x07,0x47,0x3B,0x4E,0x54,0x72,0xF4,0xEB,0x1E,0x33,0xFF,0x39,0x7E,0x54,0x76,0x17,0x8B,0x94,0x24,0x80,0x00,0x00,0x00,0x8B,0xCB,0x2B,0xD3,0x8A,0x04,0x11,0x47,0x88,0x01,0x41,0x3B,0x7E,0x54,0x72,0xF4,0x8B,0x6B,0x3C,0x33,0xC9,0x03,0xEB,0x89,0x4C,0x24,0x1C,0x33,0xD2,0x89,0x6C,0x24,0x14,0x0F,0xB7,0x45,0x14,0x66,0x3B,0x55,0x06,0x73,0x40,0x8D,0x75,0x28,0x03,0xF0,0x33,0xFF,0x39,0x3E,0x76,0x25,0x8B,0xAC,0x24,0x80,0x00,0x00,0x00,0x8B,0x46,0x04,0x8D,0x14,0x3B,0x8B,0x4E,0xFC,0x03,0xC7,0x47,0x8A,0x04,0x28,0x88,0x04,0x0A,0x3B,0x3E,0x72,0xEA,0x8B,0x6C,0x24,0x14,0x8B,0x4C,0x24,0x1C,0x0F,0xB7,0x45,0x06,0x41,0x83,0xC6,0x28,0x89,0x4C,0x24,0x1C,0x3B,0xC8,0x72,0xC5,0x6A,0x01,0x8B,0xFB,0x5E,0x89,0x74,0x24,0x20,0x2B,0x7D,0x34,0x74,0x7B,0x83,0xBD,0xA4,0x00,0x00,0x00,0x00,0x74,0x72,0x8B,0x95,0xA0,0x00,0x00,0x00,0x03,0xD3,0x83,0x3A,0x00,0x74,0x65,0x6A,0x02,0x5D,0x8D,0x72,0x08,0xEB,0x46,0x0F,0xB7,0x0E,0x66,0x8B,0xC1,0x66,0xC1,0xE8,0x0C,0x66,0x83,0xF8,0x0A,0x74,0x06,0x66,0x83,0xF8,0x03,0x75,0x0D,0x81,0xE1,0xFF,0x0F,0x00,0x00,0x03,0x0A,0x01,0x3C,0x19,0xEB,0x21,0x66,0x3B,0x44,0x24,0x20,0x75,0x07,0x8B,0xC7,0xC1,0xE8,0x10,0xEB,0x08,0x66,0x3B,0xC5,0x75,0x0E,0x0F,0xB7,0xC7,0x81,0xE1,0xFF,0x0F,0x00,0x00,0x03,0x0A,0x01,0x04,0x19,0x03,0xF5,0x8B,0x42,0x04,0x03,0xC2,0x3B,0xF0,0x75,0xB1,0x83,0x3E,0x00,0x8B,0xD6,0x75,0xA5,0x8B,0x6C,0x24,0x14,0x33,0xF6,0x46,0x83,0xBD,0x84,0x00,0x00,0x00,0x00,0x0F,0x84,0x97,0x01,0x00,0x00,0x8B,0x85,0x80,0x00,0x00,0x00,0x8D,0x0C,0x18,0x83,0xC0,0x0C,0x03,0xC3,0x89,0x4C,0x24,0x1C,0x33,0xC9,0x89,0x4C,0x24,0x18,0x39,0x08,0x74,0x0D,0x8D,0x40,0x14,0x41,0x83,0x38,0x00,0x75,0xF7,0x89,0x4C,0x24,0x18,0x8B,0x94,0x24,0x90,0x00,0x00,0x00,0x8B,0xC2,0x83,0xE0,0x04,0x89,0x44,0x24,0x3C,0x0F,0x84,0xAE,0x00,0x00,0x00,0x3B,0xCE,0x0F,0x86,0xA6,0x00,0x00,0x00,0xC1,0xEA,0x10,0x8D,0x41,0xFF,0x89,0x94,0x24,0x90,0x00,0x00,0x00,0x33,0xD2,0x89,0x44,0x24,0x38,0x89,0x54,0x24,0x20,0x85,0xC0,0x0F,0x84,0x92,0x00,0x00,0x00,0x8B,0x5C,0x24,0x1C,0x8B,0xAC,0x24,0x80,0x00,0x00,0x00,0x89,0x5C,0x24,0x1C,0x2B,0xCA,0x69,0xED,0xFD,0x43,0x03,0x00,0x33,0xD2,0x8D,0x7C,0x24,0x44,0xB8,0xFF,0x7F,0x00,0x00,0xF7,0xF1,0x81,0xC5,0xC3,0x9E,0x26,0x00,0x33,0xD2,0x6A,0x05,0x8D,0x48,0x01,0x8B,0xC5,0xC1,0xE8,0x10,0x25,0xFF,0x7F,0x00,0x00,0xF7,0xF1,0x8B,0x54,0x24,0x24,0x03,0xC2,0x6B,0xC0,0x14,0x59,0x6A,0x05,0x03,0xC3,0x42,0x8B,0xF0,0x89,0x54,0x24,0x24,0xF3,0xA5,0x8B,0x74,0x24,0x20,0x8B,0xF8,0x8B,0x44,0x24,0x20,0x59,0xF3,0xA5,0x6A,0x05,0x8B,0xF8,0x8D,0x74,0x24,0x48,0x59,0x83,0xC0,0x14,0xF3,0xA5,0x8B,0x4C,0x24,0x18,0x89,0x44,0x24,0x1C,0x3B,0x54,0x24,0x38,0x72,0x92,0x8B,0x5C,0x24,0x24,0x8B,0x6C,0x24,0x14,0xEB,0x0B,0x8B,0x44,0x24,0x40,0x89,0x84,0x24,0x90,0x00,0x00,0x00,0x8B,0xB5,0x80,0x00,0x00,0x00,0x03,0xF3,0x89,0x74,0x24,0x20,0x8B,0x46,0x0C,0x85,0xC0,0x0F,0x84,0x88,0x00,0x00,0x00,0x8B,0x6C,0x24,0x18,0x03,0xC3,0x50,0xFF,0x54,0x24,0x2C,0x8B,0x7E,0x10,0x89,0x44,0x24,0x38,0x03,0xFB,0x8B,0x06,0x03,0xC3,0x89,0x44,0x24,0x24,0x8B,0x08,0x85,0xC9,0x74,0x36,0x8B,0x6C,0x24,0x38,0x8B,0x74,0x24,0x2C,0x79,0x05,0x0F,0xB7,0xC1,0xEB,0x05,0x8D,0x41,0x02,0x03,0xC3,0x50,0x55,0xFF,0xD6,0x89,0x07,0x83,0xC7,0x04,0x8B,0x44,0x24,0x24,0x83,0xC0,0x04,0x89,0x44,0x24,0x24,0x8B,0x08,0x85,0xC9,0x75,0xDA,0x8B,0x74,0x24,0x20,0x8B,0x6C,0x24,0x18,0x83,0x7C,0x24,0x3C,0x00,0x74,0x17,0x33,0xC0,0x40,0x3B,0xE8,0x76,0x10,0x69,0x84,0x24,0x90,0x00,0x00,0x00,0xE8,0x03,0x00,0x00,0x50,0xFF,0x54,0x24,0x44,0x8B,0x46,0x20,0x83,0xC6,0x14,0x89,0x74,0x24,0x20,0x85,0xC0,0x75,0x80,0x8B,0x6C,0x24,0x14,0x83,0xBD,0xE4,0x00,0x00,0x00,0x00,0x74,0x73,0x8B,0xBD,0xE0,0x00,0x00,0x00,0x83,0xC7,0x04,0x03,0xFB,0x89,0x7C,0x24,0x20,0x83,0x3F,0x00,0x74,0x5F,0x8B,0x07,0x03,0xC3,0x50,0xFF,0x54,0x24,0x2C,0x8B,0x77,0x08,0x8B,0xE8,0x8B,0x47,0x0C,0x03,0xF3,0x03,0xC3,0x89,0x44,0x24,0x24,0x83,0x3E,0x00,0x74,0x31,0x8B,0x7C,0x24,0x2C,0x8B,0x00,0x85,0xC0,0x79,0x05,0x0F,0xB7,0xC0,0xEB,0x05,0x83,0xC0,0x02,0x03,0xC3,0x50,0x55,0xFF,0xD7,0x89,0x06,0x83,0xC6,0x04,0x8B,0x44,0x24,0x24,0x83,0xC0,0x04,0x89,0x44,0x24,0x24,0x83,0x3E,0x00,0x75,0xD7,0x8B,0x7C,0x24,0x20,0x83,0xC7,0x20,0x89,0x7C,0x24,0x20,0x83,0x3F,0x00,0x75,0xA5,0x8B,0x6C,0x24,0x14,0x0F,0xB7,0x45,0x14,0x33,0xC9,0x33,0xFF,0x66,0x3B,0x4D,0x06,0x0F,0x83,0xB0,0x00,0x00,0x00,0x8D,0x75,0x3C,0x03,0xF0,0x83,0x7E,0xEC,0x00,0x0F,0x84,0x91,0x00,0x00,0x00,0x8B,0x0E,0x33,0xD2,0x42,0x8B,0xC1,0xC1,0xE8,0x1D,0x23,0xC2,0x8B,0xD1,0xC1,0xEA,0x1E,0x83,0xE2,0x01,0xC1,0xE9,0x1F,0x85,0xC0,0x75,0x18,0x85,0xD2,0x75,0x0D,0x6A,0x08,0x58,0x6A,0x01,0x85,0xC9,0x59,0x0F,0x44,0xC1,0xEB,0x3D,0x6A,0x04,0x58,0x6A,0x02,0xEB,0xF1,0x85,0xD2,0x75,0x1E,0x85,0xC9,0x75,0x05,0x6A,0x10,0x58,0xEB,0x29,0x85,0xD2,0x75,0x11,0x85,0xC9,0x74,0x07,0xB8,0x80,0x00,0x00,0x00,0xEB,0x1A,0x8B,0x44,0x24,0x10,0xEB,0x18,0x85,0xC9,0x75,0x04,0x6A,0x20,0xEB,0xE0,0x8B,0x44,0x24,0x10,0x85,0xC9,0x6A,0x40,0x5A,0x0F,0x45,0xC2,0x89,0x44,0x24,0x10,0xF7,0x06,0x00,0x00,0x00,0x04,0x74,0x09,0x0D,0x00,0x02,0x00,0x00,0x89,0x44,0x24,0x10,0x8D,0x4C,0x24,0x10,0x51,0x50,0x8B,0x46,0xE8,0xFF,0x76,0xEC,0x03,0xC3,0x50,0xFF,0x54,0x24,0x40,0x0F,0xB7,0x45,0x06,0x47,0x83,0xC6,0x28,0x3B,0xF8,0x0F,0x82,0x55,0xFF,0xFF,0xFF,0x6A,0x00,0x6A,0x00,0x6A,0xFF,0xFF,0x54,0x24,0x40,0x83,0xBD,0xC4,0x00,0x00,0x00,0x00,0x74,0x26,0x8B,0x85,0xC0,0x00,0x00,0x00,0x8B,0x74,0x18,0x0C,0x8B,0x06,0x85,0xC0,0x74,0x16,0x33,0xED,0x45,0x6A,0x00,0x55,0x53,0xFF,0xD0,0x8D,0x76,0x04,0x8B,0x06,0x85,0xC0,0x75,0xF1,0x8B,0x6C,0x24,0x14,0x33,0xC0,0x40,0x50,0x50,0x8B,0x45,0x28,0x53,0x03,0xC3,0xFF,0xD0,0x83,0xBC,0x24,0x84,0x00,0x00,0x00,0x00,0x0F,0x84,0xAD,0x00,0x00,0x00,0x83,0x7D,0x7C,0x00,0x0F,0x84,0xA3,0x00,0x00,0x00,0x8B,0x55,0x78,0x03,0xD3,0x8B,0x6A,0x18,0x85,0xED,0x0F,0x84,0x93,0x00,0x00,0x00,0x83,0x7A,0x14,0x00,0x0F,0x84,0x89,0x00,0x00,0x00,0x8B,0x7A,0x20,0x8B,0x4A,0x24,0x03,0xFB,0x83,0x64,0x24,0x34,0x00,0x03,0xCB,0x85,0xED,0x74,0x76,0x8B,0x37,0xC7,0x44,0x24,0x1C,0x00,0x00,0x00,0x00,0x03,0xF3,0x74,0x68,0x8A,0x06,0x84,0xC0,0x74,0x1C,0x8B,0x6C,0x24,0x1C,0x0F,0xBE,0xC0,0x03,0xC5,0xC1,0xC8,0x0D,0x46,0x8B,0xE8,0x8A,0x06,0x84,0xC0,0x75,0xEF,0x89,0x6C,0x24,0x1C,0x8B,0x6A,0x18,0x8B,0x84,0x24,0x84,0x00,0x00,0x00,0x3B,0x44,0x24,0x1C,0x75,0x04,0x85,0xC9,0x75,0x15,0x8B,0x44,0x24,0x34,0x83,0xC7,0x04,0x40,0x83,0xC1,0x02,0x89,0x44,0x24,0x34,0x3B,0xC5,0x72,0xAC,0xEB,0x20,0x0F,0xB7,0x09,0x8B,0x42,0x1C,0xFF,0xB4,0x24,0x8C,0x00,0x00,0x00,0xFF,0xB4,0x24,0x8C,0x00,0x00,0x00,0x8D,0x04,0x88,0x8B,0x04,0x18,0x03,0xC3,0xFF,0xD0,0x59,0x59,0x8B,0xC3,0xEB,0x02,0x33,0xC0,0x5F,0x5E,0x5D,0x5B,0x83,0xC4,0x6C,0xC3,0x83,0xEC,0x10,0x64,0xA1,0x30,0x00,0x00,0x00,0x53,0x55,0x56,0x8B,0x40,0x0C,0x57,0x89,0x4C,0x24,0x18,0x8B,0x70,0x0C,0xE9,0x8A,0x00,0x00,0x00,0x8B,0x46,0x30,0x33,0xC9,0x8B,0x5E,0x2C,0x8B,0x36,0x89,0x44,0x24,0x14,0x8B,0x42,0x3C,0x8B,0x6C,0x10,0x78,0x89,0x6C,0x24,0x10,0x85,0xED,0x74,0x6D,0xC1,0xEB,0x10,0x33,0xFF,0x85,0xDB,0x74,0x1F,0x8B,0x6C,0x24,0x14,0x8A,0x04,0x2F,0xC1,0xC9,0x0D,0x3C,0x61,0x0F,0xBE,0xC0,0x7C,0x03,0x83,0xC1,0xE0,0x03,0xC8,0x47,0x3B,0xFB,0x72,0xE9,0x8B,0x6C,0x24,0x10,0x8B,0x44,0x2A,0x20,0x33,0xDB,0x8B,0x7C,0x2A,0x18,0x03,0xC2,0x89,0x7C,0x24,0x14,0x85,0xFF,0x74,0x31,0x8B,0x28,0x33,0xFF,0x03,0xEA,0x83,0xC0,0x04,0x89,0x44,0x24,0x1C,0x0F,0xBE,0x45,0x00,0xC1,0xCF,0x0D,0x03,0xF8,0x45,0x80,0x7D,0xFF,0x00,0x75,0xF0,0x8D,0x04,0x0F,0x3B,0x44,0x24,0x18,0x74,0x20,0x8B,0x44,0x24,0x1C,0x43,0x3B,0x5C,0x24,0x14,0x72,0xCF,0x8B,0x56,0x18,0x85,0xD2,0x0F,0x85,0x6B,0xFF,0xFF,0xFF,0x33,0xC0,0x5F,0x5E,0x5D,0x5B,0x83,0xC4,0x10,0xC3,0x8B,0x74,0x24,0x10,0x8B,0x44,0x16,0x24,0x8D,0x04,0x58,0x0F,0xB7,0x0C,0x10,0x8B,0x44,0x16,0x1C,0x8D,0x04,0x88,0x8B,0x04,0x10,0x03,0xC2,0xEB,0xDB };
            var rdiShellcode64 = new byte[] { 0x48,0x8B,0xC4,0x48,0x89,0x58,0x08,0x44,0x89,0x48,0x20,0x4C,0x89,0x40,0x18,0x89,0x50,0x10,0x55,0x56,0x57,0x41,0x54,0x41,0x55,0x41,0x56,0x41,0x57,0x48,0x8D,0x68,0xA9,0x48,0x81,0xEC,0x90,0x00,0x00,0x00,0x48,0x8B,0xF1,0xB9,0x4C,0x77,0x26,0x07,0xE8,0xA3,0x06,0x00,0x00,0xB9,0x49,0xF7,0x02,0x78,0x48,0x89,0x45,0xB7,0x4C,0x8B,0xE0,0xE8,0x92,0x06,0x00,0x00,0xB9,0x58,0xA4,0x53,0xE5,0x48,0x89,0x45,0xBF,0x4C,0x8B,0xE8,0xE8,0x81,0x06,0x00,0x00,0xB9,0x10,0xE1,0x8A,0xC3,0x4C,0x8B,0xF8,0xE8,0x74,0x06,0x00,0x00,0xB9,0xAF,0xB1,0x5C,0x94,0x48,0x89,0x45,0xD7,0x48,0x8B,0xF8,0xE8,0x63,0x06,0x00,0x00,0xB9,0x33,0x00,0x9E,0x95,0x48,0x89,0x45,0xDF,0x48,0x8B,0xD8,0xE8,0x52,0x06,0x00,0x00,0xB9,0x44,0xF0,0x35,0xE0,0x4C,0x8B,0xF0,0xE8,0x45,0x06,0x00,0x00,0x45,0x33,0xD2,0x48,0x89,0x45,0xC7,0x4D,0x85,0xE4,0x0F,0x84,0x16,0x06,0x00,0x00,0x4D,0x85,0xED,0x0F,0x84,0x0D,0x06,0x00,0x00,0x4D,0x85,0xFF,0x0F,0x84,0x04,0x06,0x00,0x00,0x48,0x85,0xFF,0x0F,0x84,0xFB,0x05,0x00,0x00,0x48,0x85,0xDB,0x0F,0x84,0xF2,0x05,0x00,0x00,0x4D,0x85,0xF6,0x0F,0x84,0xE9,0x05,0x00,0x00,0x48,0x85,0xC0,0x0F,0x84,0xE0,0x05,0x00,0x00,0x48,0x63,0x7E,0x3C,0x48,0x03,0xFE,0x81,0x3F,0x50,0x45,0x00,0x00,0x0F,0x85,0xCD,0x05,0x00,0x00,0xB8,0x64,0x86,0x00,0x00,0x66,0x39,0x47,0x04,0x0F,0x85,0xBE,0x05,0x00,0x00,0x44,0x8B,0x47,0x38,0x45,0x8D,0x5A,0x01,0x45,0x84,0xC3,0x0F,0x85,0xAD,0x05,0x00,0x00,0x0F,0xB7,0x47,0x06,0x41,0x8B,0xDA,0x0F,0xB7,0x4F,0x14,0x85,0xC0,0x74,0x28,0x48,0x83,0xC1,0x24,0x44,0x8B,0xC8,0x48,0x03,0xCF,0x8B,0x51,0x04,0x85,0xD2,0x75,0x07,0x8B,0x11,0x41,0x03,0xD0,0xEB,0x02,0x03,0x11,0x3B,0xD3,0x0F,0x47,0xDA,0x48,0x83,0xC1,0x28,0x4D,0x2B,0xCB,0x75,0xE2,0x48,0x8D,0x4D,0xE7,0x41,0xFF,0xD6,0x8B,0x55,0xEB,0x44,0x8D,0x72,0xFF,0x44,0x03,0x77,0x50,0x8D,0x42,0xFF,0xF7,0xD0,0x48,0x8D,0x4A,0xFF,0x44,0x23,0xF0,0x8B,0xC3,0x48,0x03,0xC8,0x48,0x8D,0x42,0xFF,0x48,0xF7,0xD0,0x48,0x23,0xC8,0x4C,0x3B,0xF1,0x0F,0x85,0x40,0x05,0x00,0x00,0x48,0x8B,0x4F,0x30,0x41,0xB9,0x04,0x00,0x00,0x00,0x41,0xB8,0x00,0x30,0x00,0x00,0x41,0x8B,0xD6,0x41,0xFF,0xD7,0x48,0x8B,0xD8,0x48,0x85,0xC0,0x75,0x15,0x44,0x8D,0x48,0x04,0x41,0xB8,0x00,0x30,0x00,0x00,0x41,0x8B,0xD6,0x33,0xC9,0x41,0xFF,0xD7,0x48,0x8B,0xD8,0x44,0x8B,0x5D,0x7F,0x41,0xBE,0x01,0x00,0x00,0x00,0x45,0x84,0xDE,0x0F,0x84,0xB1,0x00,0x00,0x00,0x8B,0x46,0x3C,0x89,0x43,0x3C,0x8B,0x56,0x3C,0xEB,0x0B,0x8B,0xCA,0x41,0x03,0xD6,0x8A,0x04,0x31,0x88,0x04,0x19,0x3B,0x57,0x54,0x72,0xF0,0x45,0x33,0xFF,0x48,0x63,0x7B,0x3C,0x45,0x8B,0xD7,0x48,0x03,0xFB,0x48,0x89,0x7D,0xCF,0x0F,0xB7,0x47,0x14,0x66,0x44,0x3B,0x7F,0x06,0x73,0x3E,0x4C,0x8D,0x47,0x28,0x4C,0x03,0xC0,0x45,0x8B,0xCF,0x45,0x39,0x38,0x76,0x1F,0x41,0x8B,0x50,0x04,0x41,0x8B,0x48,0xFC,0x41,0x8B,0xC1,0x45,0x03,0xCE,0x48,0x03,0xC8,0x48,0x03,0xD0,0x8A,0x04,0x32,0x88,0x04,0x19,0x45,0x3B,0x08,0x72,0xE1,0x0F,0xB7,0x47,0x06,0x45,0x03,0xD6,0x49,0x83,0xC0,0x28,0x44,0x3B,0xD0,0x72,0xC9,0x4C,0x8B,0xD3,0x4C,0x2B,0x57,0x30,0x0F,0x84,0xDE,0x00,0x00,0x00,0x44,0x39,0xBF,0xB4,0x00,0x00,0x00,0x0F,0x84,0xD1,0x00,0x00,0x00,0x44,0x8B,0x87,0xB0,0x00,0x00,0x00,0x4C,0x03,0xC3,0x45,0x39,0x38,0x0F,0x84,0xBE,0x00,0x00,0x00,0x41,0xBC,0x02,0x00,0x00,0x00,0x4D,0x8D,0x48,0x08,0xE9,0x93,0x00,0x00,0x00,0x45,0x33,0xFF,0x41,0x8B,0xD7,0x44,0x39,0x7F,0x54,0x0F,0x86,0x5D,0xFF,0xFF,0xFF,0x8B,0xCA,0x41,0x03,0xD6,0x8A,0x04,0x31,0x88,0x04,0x19,0x3B,0x57,0x54,0x72,0xF0,0xE9,0x48,0xFF,0xFF,0xFF,0x41,0x0F,0xB7,0x01,0x0F,0xB7,0xC8,0x66,0xC1,0xE9,0x0C,0x66,0x83,0xF9,0x0A,0x75,0x11,0x41,0x8B,0x08,0x25,0xFF,0x0F,0x00,0x00,0x48,0x03,0xC3,0x4C,0x01,0x14,0x01,0xEB,0x49,0x66,0x83,0xF9,0x03,0x75,0x0E,0x25,0xFF,0x0F,0x00,0x00,0x48,0x8D,0x0C,0x03,0x41,0x8B,0xC2,0xEB,0x2E,0x66,0x41,0x3B,0xCE,0x75,0x15,0x25,0xFF,0x0F,0x00,0x00,0x48,0x8D,0x0C,0x03,0x49,0x8B,0xC2,0x48,0xC1,0xE8,0x10,0x0F,0xB7,0xC0,0xEB,0x13,0x66,0x41,0x3B,0xCC,0x75,0x14,0x25,0xFF,0x0F,0x00,0x00,0x48,0x8D,0x0C,0x03,0x41,0x0F,0xB7,0xC2,0x41,0x8B,0x10,0x48,0x01,0x04,0x0A,0x4D,0x03,0xCC,0x41,0x8B,0x40,0x04,0x49,0x03,0xC0,0x4C,0x3B,0xC8,0x75,0x86,0x4D,0x8B,0xC1,0x45,0x39,0x39,0x0F,0x85,0x4C,0xFF,0xFF,0xFF,0x4C,0x8B,0x65,0xB7,0x44,0x39,0xBF,0x94,0x00,0x00,0x00,0x0F,0x84,0x45,0x01,0x00,0x00,0x44,0x8B,0x87,0x90,0x00,0x00,0x00,0x45,0x8B,0xEF,0x4C,0x03,0xC3,0x49,0x8D,0x40,0x0C,0xEB,0x07,0x45,0x03,0xEE,0x48,0x8D,0x40,0x14,0x44,0x39,0x38,0x75,0xF4,0x41,0x8B,0xC3,0x83,0xE0,0x04,0x89,0x45,0xB3,0x0F,0x84,0x82,0x00,0x00,0x00,0x45,0x3B,0xEE,0x76,0x7D,0x41,0xC1,0xEB,0x10,0x45,0x8D,0x4D,0xFF,0x44,0x89,0x5D,0x7F,0x45,0x8B,0xDF,0x45,0x85,0xC9,0x74,0x6F,0x4D,0x8B,0xD0,0x41,0x0F,0x10,0x02,0x33,0xD2,0x41,0x8B,0xCD,0x41,0x2B,0xCB,0x69,0xF6,0xFD,0x43,0x03,0x00,0xB8,0xFF,0x7F,0x00,0x00,0xF7,0xF1,0x33,0xD2,0x81,0xC6,0xC3,0x9E,0x26,0x00,0x41,0x8D,0x0C,0x06,0x8B,0xC6,0xC1,0xE8,0x10,0x25,0xFF,0x7F,0x00,0x00,0xF7,0xF1,0x41,0x03,0xC3,0x45,0x03,0xDE,0x48,0x8D,0x0C,0x80,0x41,0x8B,0x54,0x88,0x10,0x41,0x0F,0x10,0x0C,0x88,0x41,0x0F,0x11,0x04,0x88,0x41,0x8B,0x42,0x10,0x41,0x89,0x44,0x88,0x10,0x41,0x0F,0x11,0x0A,0x41,0x89,0x52,0x10,0x4D,0x8D,0x52,0x14,0x45,0x3B,0xD9,0x72,0x9C,0xEB,0x06,0x8B,0x45,0xB3,0x89,0x45,0x7F,0x8B,0xB7,0x90,0x00,0x00,0x00,0x48,0x03,0xF3,0x8B,0x46,0x0C,0x85,0xC0,0x74,0x7B,0x8B,0x7D,0x7F,0x8B,0xC8,0x48,0x03,0xCB,0x41,0xFF,0xD4,0x44,0x8B,0x3E,0x4C,0x8B,0xE0,0x44,0x8B,0x76,0x10,0x4C,0x03,0xFB,0x4C,0x03,0xF3,0x49,0x8B,0x0F,0x48,0x85,0xC9,0x74,0x2D,0x48,0x8B,0x7D,0xBF,0x79,0x05,0x0F,0xB7,0xD1,0xEB,0x07,0x48,0x8D,0x51,0x02,0x48,0x03,0xD3,0x49,0x8B,0xCC,0xFF,0xD7,0x49,0x83,0xC7,0x08,0x49,0x89,0x06,0x49,0x83,0xC6,0x08,0x49,0x8B,0x0F,0x48,0x85,0xC9,0x75,0xDA,0x8B,0x7D,0x7F,0x45,0x33,0xFF,0x44,0x39,0x7D,0xB3,0x74,0x0F,0x41,0x83,0xFD,0x01,0x76,0x09,0x69,0xCF,0xE8,0x03,0x00,0x00,0xFF,0x55,0xC7,0x8B,0x46,0x20,0x48,0x83,0xC6,0x14,0x4C,0x8B,0x65,0xB7,0x85,0xC0,0x75,0x8C,0x48,0x8B,0x7D,0xCF,0x4C,0x8B,0x6D,0xBF,0x44,0x39,0xBF,0xF4,0x00,0x00,0x00,0x74,0x68,0x44,0x8B,0xB7,0xF0,0x00,0x00,0x00,0x49,0x83,0xC6,0x04,0x4C,0x03,0xF3,0xEB,0x53,0x41,0x8B,0x0E,0x48,0x03,0xCB,0x41,0xFF,0xD4,0x41,0x8B,0x76,0x08,0x4C,0x8B,0xE0,0x45,0x8B,0x7E,0x0C,0x48,0x03,0xF3,0x4C,0x03,0xFB,0xEB,0x25,0x49,0x8B,0x0F,0x48,0x85,0xC9,0x79,0x05,0x0F,0xB7,0xD1,0xEB,0x07,0x48,0x8D,0x51,0x02,0x48,0x03,0xD3,0x49,0x8B,0xCC,0x41,0xFF,0xD5,0x48,0x89,0x06,0x48,0x83,0xC6,0x08,0x49,0x83,0xC7,0x08,0x33,0xC0,0x48,0x39,0x06,0x75,0xD4,0x4C,0x8B,0x65,0xB7,0x49,0x83,0xC6,0x20,0x45,0x33,0xFF,0x45,0x39,0x3E,0x75,0xA8,0x45,0x8B,0xF7,0x0F,0xB7,0x47,0x14,0x41,0xBC,0x01,0x00,0x00,0x00,0x66,0x44,0x3B,0x7F,0x06,0x0F,0x83,0xCF,0x00,0x00,0x00,0x4C,0x8B,0x7D,0xD7,0x48,0x8D,0x77,0x3C,0x48,0x03,0xF0,0x45,0x33,0xC9,0x44,0x39,0x4E,0xEC,0x0F,0x84,0xA0,0x00,0x00,0x00,0x8B,0x0E,0x8B,0xD1,0xC1,0xEA,0x1E,0x8B,0xC1,0x41,0x23,0xD4,0xC1,0xE8,0x1D,0xC1,0xE9,0x1F,0x41,0x23,0xC4,0x75,0x24,0x85,0xD2,0x75,0x0E,0xF7,0xD9,0x45,0x1B,0xC0,0x41,0x83,0xE0,0x07,0x45,0x03,0xC4,0xEB,0x4F,0xF7,0xD9,0xB8,0x02,0x00,0x00,0x00,0x45,0x1B,0xC0,0x44,0x23,0xC0,0x44,0x03,0xC0,0xEB,0x3D,0x85,0xD2,0x75,0x20,0x85,0xC9,0x75,0x06,0x44,0x8D,0x42,0x10,0xEB,0x2F,0x85,0xD2,0x75,0x12,0x85,0xC9,0x74,0x08,0x41,0xB8,0x80,0x00,0x00,0x00,0xEB,0x1F,0x44,0x8B,0x45,0xAF,0xEB,0x1D,0x85,0xC9,0x75,0x06,0x44,0x8D,0x41,0x20,0xEB,0x0F,0x44,0x8B,0x45,0xAF,0x85,0xC9,0xB8,0x40,0x00,0x00,0x00,0x44,0x0F,0x45,0xC0,0x44,0x89,0x45,0xAF,0xF7,0x06,0x00,0x00,0x00,0x04,0x74,0x09,0x41,0x0F,0xBA,0xE8,0x09,0x44,0x89,0x45,0xAF,0x8B,0x4E,0xE8,0x4C,0x8D,0x4D,0xAF,0x8B,0x56,0xEC,0x48,0x03,0xCB,0x41,0xFF,0xD7,0x45,0x33,0xC9,0x0F,0xB7,0x47,0x06,0x45,0x03,0xF4,0x48,0x83,0xC6,0x28,0x44,0x3B,0xF0,0x0F,0x82,0x42,0xFF,0xFF,0xFF,0x45,0x33,0xFF,0x45,0x33,0xC0,0x33,0xD2,0x48,0x83,0xC9,0xFF,0xFF,0x55,0xDF,0x44,0x39,0xBF,0xD4,0x00,0x00,0x00,0x74,0x24,0x8B,0x87,0xD0,0x00,0x00,0x00,0x48,0x8B,0x74,0x18,0x18,0xEB,0x0F,0x45,0x33,0xC0,0x41,0x8B,0xD4,0x48,0x8B,0xCB,0xFF,0xD0,0x48,0x8D,0x76,0x08,0x48,0x8B,0x06,0x48,0x85,0xC0,0x75,0xE9,0x8B,0x47,0x28,0x4D,0x8B,0xC4,0x48,0x03,0xC3,0x41,0x8B,0xD4,0x48,0x8B,0xCB,0xFF,0xD0,0x8B,0x75,0x67,0x85,0xF6,0x0F,0x84,0x96,0x00,0x00,0x00,0x44,0x39,0xBF,0x8C,0x00,0x00,0x00,0x0F,0x84,0x89,0x00,0x00,0x00,0x8B,0x8F,0x88,0x00,0x00,0x00,0x48,0x03,0xCB,0x44,0x8B,0x59,0x18,0x45,0x85,0xDB,0x74,0x77,0x44,0x39,0x79,0x14,0x74,0x71,0x44,0x8B,0x49,0x20,0x41,0x8B,0xFF,0x8B,0x51,0x24,0x4C,0x03,0xCB,0x48,0x03,0xD3,0x45,0x85,0xDB,0x74,0x5C,0x45,0x8B,0x01,0x45,0x8B,0xD7,0x4C,0x03,0xC3,0x74,0x51,0xEB,0x10,0x0F,0xBE,0xC0,0x41,0x03,0xC2,0x44,0x8B,0xD0,0x41,0xC1,0xCA,0x0D,0x4D,0x03,0xC4,0x41,0x8A,0x00,0x84,0xC0,0x75,0xE9,0x41,0x3B,0xF2,0x75,0x05,0x48,0x85,0xD2,0x75,0x16,0xB8,0x02,0x00,0x00,0x00,0x41,0x03,0xFC,0x48,0x03,0xD0,0x49,0x83,0xC1,0x04,0x41,0x3B,0xFB,0x73,0x1A,0xEB,0xBC,0x8B,0x49,0x1C,0x0F,0xB7,0x12,0x48,0x03,0xCB,0x8B,0x04,0x91,0x8B,0x55,0x77,0x48,0x03,0xC3,0x48,0x8B,0x4D,0x6F,0xFF,0xD0,0x48,0x8B,0xC3,0xEB,0x02,0x33,0xC0,0x48,0x8B,0x9C,0x24,0xD0,0x00,0x00,0x00,0x48,0x81,0xC4,0x90,0x00,0x00,0x00,0x41,0x5F,0x41,0x5E,0x41,0x5D,0x41,0x5C,0x5F,0x5E,0x5D,0xC3,0xCC,0xCC,0x48,0x89,0x5C,0x24,0x08,0x48,0x89,0x74,0x24,0x10,0x57,0x48,0x83,0xEC,0x10,0x65,0x48,0x8B,0x04,0x25,0x60,0x00,0x00,0x00,0x8B,0xF1,0x48,0x8B,0x50,0x18,0x4C,0x8B,0x4A,0x10,0x4D,0x8B,0x41,0x30,0x4D,0x85,0xC0,0x0F,0x84,0xB4,0x00,0x00,0x00,0x41,0x0F,0x10,0x41,0x58,0x49,0x63,0x40,0x3C,0x33,0xD2,0x4D,0x8B,0x09,0xF3,0x0F,0x7F,0x04,0x24,0x42,0x8B,0x9C,0x00,0x88,0x00,0x00,0x00,0x85,0xDB,0x74,0xD4,0x48,0x8B,0x04,0x24,0x48,0xC1,0xE8,0x10,0x44,0x0F,0xB7,0xD0,0x45,0x85,0xD2,0x74,0x21,0x48,0x8B,0x4C,0x24,0x08,0x45,0x8B,0xDA,0x0F,0xBE,0x01,0xC1,0xCA,0x0D,0x80,0x39,0x61,0x7C,0x03,0x83,0xC2,0xE0,0x03,0xD0,0x48,0xFF,0xC1,0x49,0x83,0xEB,0x01,0x75,0xE7,0x4D,0x8D,0x14,0x18,0x33,0xC9,0x41,0x8B,0x7A,0x20,0x49,0x03,0xF8,0x41,0x39,0x4A,0x18,0x76,0x8F,0x8B,0x1F,0x45,0x33,0xDB,0x49,0x03,0xD8,0x48,0x8D,0x7F,0x04,0x0F,0xBE,0x03,0x48,0xFF,0xC3,0x41,0xC1,0xCB,0x0D,0x44,0x03,0xD8,0x80,0x7B,0xFF,0x00,0x75,0xED,0x41,0x8D,0x04,0x13,0x3B,0xC6,0x74,0x0D,0xFF,0xC1,0x41,0x3B,0x4A,0x18,0x72,0xD1,0xE9,0x5B,0xFF,0xFF,0xFF,0x41,0x8B,0x42,0x24,0x03,0xC9,0x49,0x03,0xC0,0x0F,0xB7,0x14,0x01,0x41,0x8B,0x4A,0x1C,0x49,0x03,0xC8,0x8B,0x04,0x91,0x49,0x03,0xC0,0xEB,0x02,0x33,0xC0,0x48,0x8B,0x5C,0x24,0x20,0x48,0x8B,0x74,0x24,0x28,0x48,0x83,0xC4,0x10,0x5F,0xC3 };
            //MARKER:E
        
        var newShellcode = new List<byte>();

        uint dllOffset = 0;

        if (Is64BitDLL(dllBytes))
        {
            var rdiShellcode = rdiShellcode64;
            int bootstrapSize = 64;

            // call next instruction (Pushes next instruction address to stack)
            newShellcode.Add(0xe8);
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);

            // Set the offset to our DLL from pop result
            dllOffset = (uint)(bootstrapSize - newShellcode.Count + rdiShellcode.Length);

            // pop rcx - Capture our current location in memory
            newShellcode.Add(0x59);

            // mov r8, rcx - copy our location in memory to r8 before we start modifying RCX
            newShellcode.Add(0x49);
            newShellcode.Add(0x89);
            newShellcode.Add(0xc8);

            // Setup the location of the DLL into RCX
            // add rcx, <Offset of the DLL>
            newShellcode.Add(0x48);
            newShellcode.Add(0x81);
            newShellcode.Add(0xc1);
            foreach (byte b in BitConverter.GetBytes(dllOffset))
                newShellcode.Add(b);

            // mov edx, <hash of function>
            newShellcode.Add(0xba);
            foreach (byte b in BitConverter.GetBytes(functionHash))
                newShellcode.Add(b);

            // Put the location of our user data in 
            // add r8, <Offset of the DLL> + <Length of DLL>
            newShellcode.Add(0x49);
            newShellcode.Add(0x81);
            newShellcode.Add(0xc0);
            foreach (byte b in BitConverter.GetBytes((uint)(dllOffset + dllBytes.Length)))
                newShellcode.Add(b);

            // mov r9d, <Length of User Data>
            newShellcode.Add(0x41);
            newShellcode.Add(0xb9);
            foreach (byte b in BitConverter.GetBytes((uint)userData.Length))
                newShellcode.Add(b);

            // push rsi - save original value
            newShellcode.Add(0x56);

            // mov rsi, rsp - store our current stack pointer for later
            newShellcode.Add(0x48);
            newShellcode.Add(0x89);
            newShellcode.Add(0xe6);

            // and rsp, 0x0FFFFFFFFFFFFFFF0 - Align the stack to 16 bytes
            newShellcode.Add(0x48);
            newShellcode.Add(0x83);
            newShellcode.Add(0xe4);
            newShellcode.Add(0xf0);

            // sub rsp, 0x30 - Create some breathing room on the stack 
            newShellcode.Add(0x48);
            newShellcode.Add(0x83);
            newShellcode.Add(0xec);
            newShellcode.Add(6 * 8); // 32 bytes for shadow space + 8 bytes for last arg + 8 bytes for stack alignment

            // mov dword ptr [rsp + 0x20], <Flags> - Push arg 5 just above shadow space
            newShellcode.Add(0xc7);
            newShellcode.Add(0x44);
            newShellcode.Add(0x24);
            newShellcode.Add(4 * 8);
            foreach (byte b in BitConverter.GetBytes((uint)flags))
                newShellcode.Add(b);

            // call - Transfer execution to the RDI
            newShellcode.Add(0xe8);
            newShellcode.Add((byte)(bootstrapSize - newShellcode.Count - 4)); // Skip over the remainder of instructions
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);

            // mov rsp, rsi - Reset our original stack pointer
            newShellcode.Add(0x48);
            newShellcode.Add(0x89);
            newShellcode.Add(0xf4);

            // pop rsi - Put things back where we left them
            newShellcode.Add(0x5e);

            // ret - return to caller
            newShellcode.Add(0xc3);

            // Write the rest of RDI
            foreach (byte b in rdiShellcode)
                newShellcode.Add(b);

        }
        else // 32 Bit
        {
            var rdiShellcode = rdiShellcode32;
            int bootstrapSize = 46;

            // call next instruction (Pushes next instruction address to stack)
            newShellcode.Add(0xe8);
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);

            // Set the offset to our DLL from pop result
            dllOffset = (uint)(bootstrapSize - newShellcode.Count + rdiShellcode.Length);

            // pop eax - Capture our current location in memory
            newShellcode.Add(0x58);

            // push ebp
            newShellcode.Add(0x55);

            // mov ebp, esp
            newShellcode.Add(0x89);                
            newShellcode.Add(0xe5);

            // mov ebx, eax - copy our location in memory to ebx before we start modifying eax
            newShellcode.Add(0x89);
            newShellcode.Add(0xc3);

            // add eax, <Offset to the DLL>
            newShellcode.Add(0x05);
            foreach (byte b in BitConverter.GetBytes(dllOffset))
                newShellcode.Add(b);

            // add ebx, <Offset to the DLL> + <Size of DLL>
            newShellcode.Add(0x81);
            newShellcode.Add(0xc3);
            foreach (byte b in BitConverter.GetBytes((uint)(dllOffset + dllBytes.Length)))
                newShellcode.Add(b);

            //push <Flags>
            newShellcode.Add(0x68);
            foreach (byte b in BitConverter.GetBytes(flags))
                newShellcode.Add(b);

            //push <Length of User Data>
            newShellcode.Add(0x68);
            foreach (byte b in BitConverter.GetBytes((uint)userData.Length))
                newShellcode.Add(b);

            // push ebx
            newShellcode.Add(0x53);

            // push <hash of function>
            newShellcode.Add(0x68);
            foreach (byte b in BitConverter.GetBytes(functionHash))
                newShellcode.Add(b);

            // push eax
            newShellcode.Add(0x50);

            // call - Transfer execution to the RDI
            newShellcode.Add(0xe8);
            newShellcode.Add((byte)(bootstrapSize - newShellcode.Count - 4)); // Skip over the remainder of instructions
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);
            newShellcode.Add(0x00);

            // leave
            newShellcode.Add(0xc9);

            // ret - return to caller
            newShellcode.Add(0xc3);

            //Write the rest of RDI
            foreach (byte b in rdiShellcode)
                newShellcode.Add(b);
        }
        
        byte[] finalBytes = newShellcode.ToArray();
        Array.Resize(ref finalBytes, finalBytes.Length + dllBytes.Length + userData.Length);

        //Write our DLL
        dllBytes[0] = 0x00;
        dllBytes[1] = 0x00;
        Buffer.BlockCopy(dllBytes, 0, finalBytes, newShellcode.Count, dllBytes.Length);

        //Write our userdata
        Buffer.BlockCopy(userData, 0, finalBytes, newShellcode.Count + dllBytes.Length, userData.Length);

        return finalBytes;
    }
}
"@

Function ConvertTo-Shellcode{
    <#
    .SYNOPSIS
    Convert DLL file to position independent shellcode

    Author: Nick Landers (@monoxgas)
    License: BSD 3-Clause
    Required Dependencies: None
    Optional Dependencies: Invoke-Shellcode.ps1

    .DESCRIPTION
    Uses ShellcodeRDI to bootstrap and shellcode PE loader to an existing file. When
    execution is passed to the converted shellcode, the DLL file is unpacked and loaded
    in memory.

    .EXAMPLE
    C:\PS> ConvertTo-Shellcode -File TestDLL_x86.dll

    .EXAMPLE
    C:\PS> ConvertTo-Shellcode -File MyTool.dll -FunctionName "MyExport" -UserData "MyDataForMyExport"

    .EXAMPLE
    C:\PS> import-module .\Invoke-Shellcode.ps1
    C:\PS> Invoke-Shellcode -Shellcode (ConvertTo-Shellcode TestDLL_x64.dll -ClearHeader)

    .PARAMETER File
    The target DLL file to convert

    .PARAMETER FunctionName
    Name of the exported function to call after DLLMain

    .PARAMETER UserData
    Data to pass to the target function

    .PARAMETER ClearHeader
    Clear the PE header on load

    .PARAMETER ObfuscateImports
    Randomize import dependency load order

    .PARAMETER ImportDelay
    Number of seconds to pause between loading imports (requires -ObfuscateImports)
    #>
    [CmdletBinding()]
    Param(
      [Parameter(Mandatory=$True,Position=1)]
      [string] $File,

      [Parameter(Position=2)]
      [string] $FunctionName = "SayHello",

      [Parameter(Position=3)]
      [string] $UserData = "dave",

      [switch] $ClearHeader,

      [switch] $ObfuscateImports,
      [int] $ImportDelay = 0 
    )

    $Parameters = New-Object System.CodeDom.Compiler.CompilerParameters
    $Parameters.CompilerOptions += "/unsafe"
    Add-Type -TypeDefinition $Source -Language CSharp -CompilerParameters $Parameters

    $FileData = [System.IO.File]::ReadAllBytes($File)
    $UserDataBytes =  [system.Text.Encoding]::Default.GetBytes($UserData)
    $FunctionHash = [sRDI]::HashFunction($FunctionName);

    $Flags = 0

    if($ClearHeader){
        $Flags = ($Flags -bor 0x1)
    }

    if($ObfuscateImports){
        # bitshift the delay into the high 16 bits
        $Delay = $ImportDelay * [math]::pow(2, 16)

        # bitwise or the 0x4 flag and the shifted delay
        $Flags = (($Flags -bor 0x4) -bor $Delay)
    }

    return [Byte[]][sRDI]::ConvertToShellcode($FileData, $FunctionHash, $UserDataBytes, $Flags)
}
